//
//  XMPubPriceView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/6.
//

#import "XMPubPriceView.h"

@interface XMPubPriceView ()

@property (nonatomic, strong) UIView *priceBgView;  //见面费
@property (nonatomic, strong) UITextField *priceTextField;  //见面费

@end

@implementation XMPubPriceView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEXA(0x464646, 0.66);
//    self.backgroundColor = kColorHEX(0x00ffff);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    
    [self addSubview:self.priceBgView];
    [_priceBgView addSubview:self.priceTextField];
    self.alpha = 0;
}

- (void)tapAction:(UIGestureRecognizer *)tap {
    [self endEditing:YES];
    [self disMiss];
}

- (void)show {
    [self.priceTextField becomeFirstResponder];
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
    }];
}

- (void)disMiss {
    self.priceInputBlock ? self.priceInputBlock(_priceTextField.text) : nil;
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.priceBgView.y = weakSelf.height;
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.priceBgView.y = weakSelf.height - keyboardRect.size.height - weakSelf.priceBgView.height;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [self disMiss];
}

#pragma mark - Init
- (UIView *)priceBgView {
    if (!_priceBgView) {
        _priceBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, KScaleWidth(75.0))];
        _priceBgView.backgroundColor = kColorHEX(0xffffff);
        [_priceBgView addSubview:self.priceTextField];
    }
    return _priceBgView;
}

- (UITextField *)priceTextField {
    if (!_priceTextField) {
        _priceTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(18.0), (self.priceBgView.height - KScaleWidth(43.0))/2, _priceBgView.width - KScaleWidth(18.0)*2, KScaleWidth(43.0))];
        _priceTextField.backgroundColor = kColorHEX(0xEEEEEF);
        [_priceTextField setCornerRadius:KScaleWidth(6.0)];
        _priceTextField.font = KFontSize(14.0);
        _priceTextField.textAlignment = NSTextAlignmentRight;
        _priceTextField.placeholder = @"￥0.00";
        _priceTextField.keyboardType = UIKeyboardTypeDecimalPad;
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(75.0), _priceTextField.height)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(14.0), 0, leftView.width - KScaleWidth(14.0), leftView.height) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
        titleLabel.text = @"见面费｜";
        [leftView addSubview:titleLabel];
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(17.0), _priceTextField.height)];
        _priceTextField.leftViewMode = UITextFieldViewModeAlways;
        _priceTextField.rightViewMode = UITextFieldViewModeAlways;
        _priceTextField.leftView = leftView;
        _priceTextField.rightView = rightView;
    }
    return _priceTextField;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
